package com.dingding.service.rentsale.core.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.RentSaleCarOrder;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.rentsale.dto.ResGetRentSaleOrderStatus;
import com.dingding.facde.rentsale.enums.CancelRentSaleOrderEnum;
import com.dingding.service.rentsale.core.dao.IRentSaleCarOrderDao;
import com.dingding.service.rentsale.core.sql.RentSaleCarSql;

/**
 * @Description:以租代售订单表
 * @author fengshuonan
 * @date 2016年7月4日 下午3:43:00
 * @version 叮叮搭车
 * @since 2.3.0
 */
@Component("rentSaleCarOrderDao")
public class RentSaleCarOrderDaoImpl extends BaseDaoImpl<RentSaleCarOrder> implements IRentSaleCarOrderDao{

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Description: 是否存在以租代购订单
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return boolean true 存在  false 不存在
	 * @since 2.3.0
	 */
	@Override
	@Transactional(readOnly = true)
	public boolean isExistThisOrder(String userId) {
		
		Criterion criterion = Restrictions.eq("loginId", userId);
		
		List<RentSaleCarOrder> orders = null;
		
		try {
			orders = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("判断是否存在以租代购订单时候出错！RentSaleCarOrderDaoImpl：isExistThisOrder（）",e);
		}
		
		if(orders == null || orders.size() <= 0){
			return false;
		}
		
		for(RentSaleCarOrder order : orders){
			if(order.getOrderStatus() == 1){
				return true;
			}
		}
		
		return false;
	}

	
	/**
	 * @Description:以租代售下单
	 * @param loginId 用户登录id
	 * @param vehicleId 车辆id
	 * @param cityId 城市id
	 * @author fengshuonan
	 * @return boolean true 下单成功 false 下单失败
	 * @since 2.3.0
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean placeRentSaleCarOrder(String loginId, String vehicleId, String cityId) {
		
		RentSaleCarOrder carOrder = new RentSaleCarOrder();
		
		carOrder.setRsId(ToolUtil.getUid());//设置主键id
		carOrder.setCreateTime(new Date());	//设置下单时间
		carOrder.setLoginId(loginId);		//设置用户id
		carOrder.setCityId(cityId);			//设置城市id
		carOrder.setOrderStatus(1);			//订单状态 (1:已预约 2：已取消 3：已完成)
		carOrder.setVehicleId(vehicleId);	//设置汽车id
		
		try {
			super.save(carOrder);
			return true;
		} catch (Exception e) {
			log.error("以租代售下单失败！RentSaleCarOrderDaoImpl：placeRentSaleCarOrder（）",e);
			return false;
		}
	}


	/**
	 * @Description 获取以租代售订单的详情
	 * @param userId 用户id
	 * @author fengshuonan
	 * @return ResGetRentSaleOrderStatus 以租代售订单状态封装
	 * @since 2.3.2
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ResGetRentSaleOrderStatus> getRentSaleOrder(String userId) {
		
		List<ResGetRentSaleOrderStatus> orderList = null;
		
		try {
			orderList =  (List<ResGetRentSaleOrderStatus>) super.listSqlFreeObject(
					RentSaleCarSql.GET_RENT_SALE_CAR_ORDER_HQL,ResGetRentSaleOrderStatus.class, userId);
		} catch (Exception e) {
			log.error("获取以租代售订单详情出错！RentSaleCarOrderDaoImpl：getRentSaleOrder()",e);
		}
		
		if(orderList == null || orderList.size() <= 0){
			return null;
		}
		
		return orderList;
	}


	/**
	 * @Description: 取消以租代售订单
	 * @param userId 用户id
	 * @author fengshuonan
	 * @since 2.3.2
	 */
	@Override
	public CancelRentSaleOrderEnum cancelRentSaleCarOrder(String userId) {
		
		Criterion criterion = Restrictions.eq("loginId", userId);
		
		List<RentSaleCarOrder> orders = null;
		
		try {
			orders = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("取消以租代售订单失败！RentSaleCarOrderDaoImpl ：cancelRentSaleCarOrder()",e);
		}
		
		if(orders == null){
			return CancelRentSaleOrderEnum.ORDER_NOT_EXISTED;
		}
		
		for(RentSaleCarOrder order : orders){
			if(order.getOrderStatus() == 1){
				order.setOrderStatus(2);
				return CancelRentSaleOrderEnum.CANCEL_ORDER_SUCCESS;
			}
		}
		
		return CancelRentSaleOrderEnum.ORDER_STATUS_EXCEPTION;
	}

}
